package com.ccys.cms.service.corporateAndInsurance.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.corporateAndInsurance.EapBenefitDoctorFeeService;
import com.ccys.cms.service.corporateAndInsurance.EapBenefitDoctorService;
import com.ccys.cms.service.corporateAndInsurance.EapBenefitPriceService;
import com.ccys.cms.service.corporateAndInsurance.EapService;
import com.ccys.common.exception.ParamException;
import com.ccys.common.util.Kit;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.corporateAndInsurance.*;
import com.ccys.db.entity.*;
import com.ccys.db.mapper.*;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.corporateAndInsurance.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: WhiteCoat
 * @description: EAP接口实现类
 * @author: ZhangJingCan
 * @create: 2023-12-22 15:55
 **/

@Service
public class EapServiceImpl extends BaseServiceImpl<TblEap> implements EapService {

    @Resource
    private TblEapMapper tblEapMapper;

    @Resource
    private TblEapBenefitMapper tblEapBenefitMapper;

    @Resource
    private TblEapBenefitPriceMapper tblEapBenefitPriceMapper;

    @Autowired(required = false)
    private EapBenefitPriceService eapBenefitPriceService;

    @Resource
    private TblEapBenefitDoctorMapper tblEapBenefitDoctorMapper;

    @Autowired(required = false)
    private EapBenefitDoctorService eapBenefitDoctorService;

    @Resource
    private TblEapBenefitDoctorFeeMapper tblEapBenefitDoctorFeeMapper;

    @Autowired(required = false)
    private EapBenefitDoctorFeeService eapBenefitDoctorFeeService;


    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Resource
    private TblEapBenefitServiceMapper tblEapBenefitServiceMapper;

    @Resource
    private TblEapBenefitAdjustmentMapper tblEapBenefitAdjustmentMapper;

    @Resource
    private TblBookingEapMapper tblBookingEapMapper;

    @Resource
    private TblEapBenefitLogMapper tblEapBenefitLogMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveEap(EapDto dto) {
        //校验如果存在ID - 抛出错误信息 - 不允许修改
        if (ObjectUtil.isNotEmpty(dto.getId())) {
            return ResponseResult.error("illegal request");
        }
        //校验
        Kit.allParamValid(dto, "type", "benefitId");

        //检查是否有重复的数据 - 未删除的
        Example example = new Example(TblEap.class);
        example.createCriteria()
                .andEqualTo("type", dto.getType())
                .andEqualTo("benefitId", dto.getBenefitId())
                .andEqualTo("delFlag", 0);

        //根据不同的类型做校验 - 0:retail;1:AIA corporate;2:AIA health shield;3:Direct corporate;4:Subscription
        //retail 与AIA healthshield
        if (1 == dto.getType() || 3 == dto.getType() || 4 == dto.getType()) {
            if (ObjectUtil.isEmpty(dto.getPlanId())) {
                return ResponseResult.error("Parameter abnormality");
            }
            //校验到这里就加上planId为条件查询
            example.createCriteria().andEqualTo("planId", dto.getPlanId());
        } else {
            //否则就查询这个字段为null的
            example.createCriteria().andIsNull("planId");
            //并且为了确保planId为null - 将dto的这个字段设置为null - 因为进入这里面 - 说明type为0或者2 - 这种情况planId必须为null
            dto.setPlanId(null);
        }
        //校验 - 存在就返回错误信息
        if (ObjectUtil.isNotEmpty(tblEapMapper.selectOneByExample(example))) {
            return ResponseResult.error("Same data exists, prohibited from adding!");
        }
        //到这里则直接新增 - 这个EAP页面没有修改 - 不提供修改
        if (tblEapMapper.insertSelective(dto) > 0) {
            return ResponseResult.success("Successfully added data");
        }
        return ResponseResult.error("Failed to add data");
    }

    @Override
    public ResponseResult<PageInfo<EapPageQueryVo>, Object> findPageEap(EapDto dto) {
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        PageDto.startPage();
        List<EapPageQueryVo> list = tblEapMapper.findPageEap(dto);
        PageInfo<EapPageQueryVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }

    @Override
    public ResponseResult updateEapStatus(EapDto dto) {
        //校验
        if (ObjectUtil.isEmpty(dto.getId()) || ObjectUtil.isEmpty(dto.getStatus())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备数据修改
        TblEap tblEap = new TblEap();
        tblEap.setId(dto.getId());
        tblEap.setStatus(dto.getStatus());
        if (tblEapMapper.updateByPrimaryKeySelective(tblEap) > 0) {
            //保存状态修改日志
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
            return ResponseResult.success("Modified successfully!");
        }
        return ResponseResult.error("Modification failed!");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveEapBenefit(EapBenefitDto dto) throws ParseException {
        //校验主表字段
        Kit.allParamValid(dto, "eapId", "startDate", "monthlyAccessMinutes", "patientAccess", "remainMonthlyAccess"
                , "doctorType", "companyId", "benefitPrices", "doctors");
        //判断endDate是否比startDate小 -
        if (dto.getEndDate().before(dto.getStartDate())) {
            return ResponseResult.error("Data error! The selected end time is smaller than the start time! Unable to add data!");
        }

        //校验时间区间 - 根据eapId去查询 status等于1的benefit数据 - 这个数据同时只能存在一条status - 1的数据
        //准备查询条件
        Example eapBenefitExample = new Example(TblEapBenefit.class);
        Example.Criteria criteria = eapBenefitExample.createCriteria()
                .andEqualTo("eapId", dto.getEapId())
                .andEqualTo("status", 1)
                .andEqualTo("delFlag", 0);
        //判断当前如果属于修改 - 排除自身
        if (ObjectUtil.isNotEmpty(dto.getId())) {
            criteria.andNotEqualTo("id", dto.getId());
        }
        //查询
        TblEapBenefit tblEapBenefit = tblEapBenefitMapper.selectOneByExample(eapBenefitExample);
        //判断不为空 - 需要校验开始时间和结束时间 - 时间不允许交叉
        if (ObjectUtil.isNotEmpty(tblEapBenefit)) {
            //数据存在 - 先校验当前数据的开始时间是否在数据库数据的开始和结束时间区间内 - 如果db数据没有结束时间 - 直接返回错误信息 - 没有结束时间则代表长期
            if (ObjectUtil.isEmpty(tblEapBenefit.getEndDate())) {
                return ResponseResult.error("Unable to save data as there is already a data set with an end time of long-term");
            }

            //到这里则说明这条启用数据存在 - 开始时间和结束时间 - 调用工具判断 - 如果时间在区间内会返回true
            if (Kit.isDateBetween(tblEapBenefit.getStartDate(), tblEapBenefit.getEndDate(), dto.getStartDate())) {
                return ResponseResult.error("The start time of the incoming data is within the enabled data time interval, and time crossing cannot add data!");
            }

            //能走到这里的只管设置为未启用状态
            //如果是新增 - 到这里则属于查询到了已存在的启用数据 - 所以当前dto的数据必须设置为未启用
            //如果是修改 - 修改的自身已启用的数据 - 是不可能走到这里的，上面是有排除自身的
            //如果修改的是未启用数据 - 排除自身也没用 - 到这里说明查询到启用数据 - 所以当前dto的修改数据的状态也必须设置为0
            dto.setStatus(0);
        }


        //判断是新增还是修改 -
        if (ObjectUtil.isEmpty(dto.getId())) {
            //新增 - 先添加主表
            if (tblEapBenefitMapper.insertSelective(dto) > 0) {
                //调用公共方法新增副表数据
                baseSave(dto);
                return ResponseResult.success("Added successfully");
            }
            return ResponseResult.error("Failed to add data");
        } else {
            //修改 - 需要去删除其他副表 - 软删除
            //修改主表
            if (tblEapBenefitMapper.updateByPrimaryKeySelective(dto) > 0) {
                //软删除 - TblEapBenefitPrice
                tblEapBenefitPriceMapper.softDeletionByBenefitId(dto.getId());
                //软删除 - TblEapBenefitDoctor
                tblEapBenefitDoctorMapper.softDeletionByBenefitId(dto.getId());
                //软删除 - TblEapBenefitDoctorFee
                tblEapBenefitDoctorFeeMapper.softDeletionByBenefitId(dto.getId());
                //调用公共方法新增副表数据
                baseSave(dto);
                return ResponseResult.success();
            } else {
                return ResponseResult.error("Modification failed, please try again later!");
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateEapBenefitStatus(EapBenefitDto dto) {
        //校验
        if (ObjectUtil.isEmpty(dto.getId()) || ObjectUtil.isEmpty(dto.getStatus())) {
            return ResponseResult.error("Parameter Error!");
        }
        //如果传入的状态为1-启用 - 需要检查数据库是否存在已启用数据
        if (1 == dto.getStatus()) {
//            //查询 状态为1 并且结束时间大于等于当前时间的数据?
//            tblEapBenefitMapper.findByEndDateAndByStatus()
            //直接查询状态为1并且未删除的数据
            Example example = new Example(TblEapBenefit.class);
            example.createCriteria()
                    .andEqualTo("status", 1)
                    .andEqualTo("delFlag", 0);
            //查询并且检查 - 存在则返回错误信息
            if (ObjectUtil.isNotEmpty(tblEapBenefitMapper.selectOneByExample(example))) {
                return ResponseResult.error("There is already enabled data, and only one enabled data can exist at the same time!");
            }
        }

        //准备数据修改
        TblEapBenefit tblEapBenefit = new TblEapBenefit();
        tblEapBenefit.setId(dto.getId());
        tblEapBenefit.setStatus(dto.getStatus());
        if (tblEapBenefitMapper.updateByPrimaryKeySelective(tblEapBenefit) > 0) {
            //保存状态修改日志
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
            return ResponseResult.success("Modified successfully!");
        }
        return ResponseResult.error("Modification failed!");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteEapBenefit(EapBenefitDto dto) {
        //判断
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备实体类并赋值
        TblEapBenefit tblEapBenefit = new TblEapBenefit();
        tblEapBenefit.setId(dto.getId());
        tblEapBenefit.setDelFlag(1);
        //判断是否修改成功 - 返回不同状态
        if (tblEapBenefitMapper.updateByPrimaryKeySelective(tblEapBenefit) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    public ResponseResult<List<EapBenefitListVo>, Object> findListEapBenefit(EapBenefitDto dto) {
        //检查参数 - eapId必传
        if (ObjectUtil.isEmpty(dto.getEapId())) {
            return ResponseResult.error("Parameter abnormality");
        }
        List<EapBenefitListVo> list = tblEapMapper.findListEapBenefit(dto.getEapId());
        return ResponseResult.success(list);
    }

    @Override
    public ResponseResult<EapBenefitDetailVo, Object> findEapBenefitDetail(EapBenefitDto dto) {
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter abnormality");
        }
        //这里面使用了嵌套查询 - 只剩下doctorFee未查询 - 循环查
        EapBenefitDetailVo vo = tblEapBenefitMapper.findEapBenefitDetail(dto.getId());
        if (ObjectUtil.isNotEmpty(vo.getDoctors())) {
            for (EapBenefitDoctorVo doctor : vo.getDoctors()) {
                doctor.setDoctorFees(tblEapBenefitDoctorFeeMapper.findListByBenefitIdAndByDoctorId(vo.getId(), doctor.getDoctorId()));
            }
        }
        return ResponseResult.success(vo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveEapBenefitService(EapBenefitServiceDto dto) {
        //校验
        Kit.allParamValid(dto, "benefitId", "serviceType", "subCategory", "date", "detuctType", "minutes");
        if (ObjectUtil.isEmpty(dto.getId())) {
            //新增
            if (tblEapBenefitServiceMapper.insertSelective(dto) > 0) {
                return ResponseResult.success("Successfully added data");
            }
            return ResponseResult.error("Failed to add data");
        } else {
            //修改
            if (tblEapBenefitServiceMapper.updateByPrimaryKeySelective(dto) > 0) {
                return ResponseResult.success("Data modification successful");
            }
            return ResponseResult.error("Data modification failed");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteEapBenefitService(EapBenefitServiceDto dto) {
        //判断
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备实体类并赋值
        TblEapBenefitService tblEapBenefitService = new TblEapBenefitService();
        tblEapBenefitService.setId(dto.getId());
        tblEapBenefitService.setDelFlag(1);
        //判断是否修改成功 - 返回不同状态
        if (tblEapBenefitServiceMapper.updateByPrimaryKeySelective(tblEapBenefitService) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    public ResponseResult<PageInfo<EapBenefitServicePageQueryVo>, Object> findPageEapBenefitService(EapBenefitServiceDto dto) {
        //校验必传参数
        if (ObjectUtil.isEmpty(dto.getBenefitId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //开启分页
        PageDto.startPage();
        List<EapBenefitServicePageQueryVo> list = tblEapBenefitServiceMapper.findPageEapBenefitService(dto);
        PageInfo<EapBenefitServicePageQueryVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }

    @Override
    public ResponseResult<PageInfo<MemberPatientEmployeesPageQueryVo>, Object> findPageMemberPatientEmployees(EapDto dto) {
        //校验值
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //开启分页
        PageDto.startPage();
        List<MemberPatientEmployeesPageQueryVo> list = tblEapMapper.findPageMemberPatientEmployees(dto);
        PageInfo<MemberPatientEmployeesPageQueryVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteEap(EapDto dto) {
        //判断
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备实体类并赋值
        TblEap tblEap = new TblEap();
        tblEap.setId(dto.getId());
        tblEap.setDelFlag(1);
        //判断是否修改成功 - 返回不同状态
        if (tblEapMapper.updateByPrimaryKeySelective(tblEap) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult addManualAdjustment(EapBenefitAdjustmentDto dto) {
        //校验
        Kit.allParamValid(dto, "benefitId", "month", "type", "value", "reason");
        //直接保存并返回
        return ResponseResult.update(tblEapBenefitAdjustmentMapper.insertSelective(dto));
    }

    @Override
    public ResponseResult<List<EapUsageLimitVo>, Object> findUsageLimit(EapDto dto) {
        //Benefit 为单条  - 查询出来之后判断创建时间到至今过去几个月，每个月生成一条
        TblEapBenefit tblEapBenefit = tblEapBenefitMapper.selectByPrimaryKey(dto.getBenefitId());
        if (ObjectUtil.isEmpty(tblEapBenefit)) {
            return null;
        }

        //这条sql直接将几个列的值都查询出来了
        List<EapInfoUsageUnionAllVo> unionAllVos = tblEapBenefitMapper.findEapInfoUsageUnionAll(tblEapBenefit.getId());

        //流的方式根据type分组
//        Map<String, List<EapInfoUsageUnionAllVo>> typeMap = unionAllVos.stream().collect(Collectors.groupingBy(EapInfoUsageUnionAllVo::getType));
        //先根据type分组 - 再根据month - 转换为map
        Map<String, Map<String, BigDecimal>> mapMap = unionAllVos.stream()
                .collect(Collectors.groupingBy(EapInfoUsageUnionAllVo::getType,
                        Collectors.toMap(EapInfoUsageUnionAllVo::getMonth,
                                e -> Optional.ofNullable(e.getMins()).orElse(BigDecimal.ZERO))));


        //调用方法获取创建至今过去多少月 - 有多少月就有多少条数据
        //获取到至今的所有年月的list - 这个方法是排序了的，最小的年月在前面 - 值示例:"2023-10","2023-11"....
        List<String> monthsTillNow = Kit.getMonthsTillNow(new Date(tblEapBenefit.getCreatedOnUtc()));
        List<EapUsageLimitVo> vos = new ArrayList<>();
        for (String s : monthsTillNow) {
            EapUsageLimitVo vo = new EapUsageLimitVo();
            //设置年月字符串
            vo.setDate(s);

            //对mapMap进行非空判断，防止出现空指针异常
            if (mapMap != null) {
                //获取adjustedMinutes的Map，并判断是否为空，防止出现空指针异常
                Map<String, BigDecimal> adjustedMinutesMap = mapMap.get("adjustedMinutes");
                if (adjustedMinutesMap != null) {
                    vo.setAdjustedMinutes(adjustedMinutesMap.get(s));
                }

                //获取appointmentMinute的Map，并判断是否为空，防止出现空指针异常
                Map<String, BigDecimal> appointmentMinuteMap = mapMap.get("appointmentMinute");
                if (appointmentMinuteMap != null) {
                    vo.setAppointmentMinute(appointmentMinuteMap.get(s));
                }

                //获取consultedMinute的Map，并判断是否为空，防止出现空指针异常
                Map<String, BigDecimal> consultedMinuteMap = mapMap.get("consultedMinute");
                if (consultedMinuteMap != null) {
                    vo.setConsultedMinute(consultedMinuteMap.get(s));
                }

                //获取serviceMinute的Map，并判断是否为空，防止出现空指针异常
                Map<String, BigDecimal> serviceMinuteMap = mapMap.get("serviceMinute");
                if (serviceMinuteMap != null) {
                    vo.setServiceMinute(serviceMinuteMap.get(s));
                }

                //获取serviceMinutes的Map，并判断是否为空，防止出现空指针异常
                Map<String, BigDecimal> serviceMinutesMap = mapMap.get("serviceMinutes");
                if (serviceMinutesMap != null) {
                    vo.setServiceMinutes(serviceMinutesMap.get(s));
                }
            }

            vos.add(vo);
        }


        //通过log做每条数据
        List<EapBenefitLogMakeVo> logMakes = tblEapBenefitLogMapper.findListByBenefitId(tblEapBenefit.getId());
        //流的方式转换为map - key为month
        Map<String, EapBenefitLogMakeVo> logMakeVoMap = logMakes.stream().collect(Collectors.toMap(EapBenefitLogMakeVo::getMonth, e -> e));
        //用于记录赋值的分钟数
        Integer minutes = tblEapBenefit.getMonthlyAccessMinutes();
        //循环准备当月分钟数
        //假设新建的benefit 刚开始几个月未创建log修改记录
        //循环数据是从最小的年月开始->那么minutes值一直都是本表的值->直到获取到log相匹配的年月->那么当前循环的这条数据会取值log的最大值
        //同时minutes会被更新为这个最大的log值->以此类推如果本表某个月未获取到log数据->那么minutes值最后一次更新为上次循环->
        //比如当前2023-10为最后一条log数据，最大值为500，那么minutes也会被赋值500，然后开始下一次循环2023-11->本次未获取到log->那么值也就为最后一次更改的500值
        //假设循环再次遇到log有值->那么minutes值会继续更新->以此类推
        for (EapUsageLimitVo vo : vos) {
            //判断log表是否存在 - 不存在则设置上面的值
            if (ObjectUtil.isEmpty(logMakeVoMap.get(vo.getDate()))) {
                vo.setMonthlyAccessMinutes(minutes);
            } else {
                //存在则取map 同时设置minutes的值为这个map最大值
                minutes = logMakeVoMap.get(vo.getDate()).getNewMonthlyAccessMinutes();
                vo.setMonthlyAccessMinutes(minutes);
            }
        }
        return ResponseResult.success(vos);
    }

    @Override
    public ResponseResult<List<EapUsageLimitDetailsVo>, Object> findUsageLimitDetails(EapDto dto) {
        if (ObjectUtil.isEmpty(dto.getBenefitId()) && ObjectUtil.isNotEmpty(dto.getMonth())) {
            return ResponseResult.error("Parameter abnormality");
        }

        List<EapUsageLimitDetailsVo> vos = tblEapBenefitMapper.findEapInfoUsageDetail(dto.getBenefitId(), dto.getMonth());

        return ResponseResult.success(vos);
    }


    /**
     * 公共的新增副表数据方法
     *
     * @param dto
     * @throws ParseException
     */
    @Transactional(rollbackFor = Exception.class)
    public void baseSave(EapBenefitDto dto) throws ParseException {
        //判断非空才操作这个表
        if (ObjectUtil.isNotEmpty(dto.getBenefitPrices())) {
            List<TblEapBenefitPrice> benefitPrices = dto.getBenefitPrices();
            for (TblEapBenefitPrice benefitPrice : benefitPrices) {
                //校验参数
                Kit.allParamValid(benefitPrice, "companyId", "companyPriceId", "conversionRate", "dispOrder");
                //设置关联ID
                benefitPrice.setBenefitId(dto.getId());
                //新增操作
                benefitPrice.preInsert();
            }
            //批量新增
            eapBenefitPriceService.insertAll(benefitPrices);
        } else {
            throw new ParamException("Parameter abnormality");
        }
        //判断医生数据是否存在 - 存在才操作
        if (ObjectUtil.isNotEmpty(dto.getDoctors())) {
            //医生数据
            List<EapBenefitDoctorDto> doctors = dto.getDoctors();
            //医生费用数据
            List<TblEapBenefitDoctorFee> fees = new ArrayList<>();

            //循环医生数据
            for (EapBenefitDoctorDto doctor : doctors) {
                //校验参数
                Kit.allParamValid(doctor, "doctorId", "allowOnDemandConsultation", "allowInClinicAppointment");
                //设置关联ID
                doctor.setBenefitId(dto.getId());
                doctor.preInsert();
                //获取到医生费用数据
                if (ObjectUtil.isNotEmpty(doctor.getDoctorFees())) {
                    List<TblEapBenefitDoctorFee> doctorFees = doctor.getDoctorFees();
                    //循环费用数据 - 这里主要做新增之前的准备
                    for (TblEapBenefitDoctorFee doctorFee : doctorFees) {
                        //校验数据
                        Kit.allParamValid(doctorFee, "doctorId", "doctorFeeId", "rateMinutes");
                        //关联ID
                        doctorFee.setBenefitId(dto.getId());
                        //新增准备
                        doctorFee.preInsert();
                        //添加进集合 - 到时候一次性批量新增
                        fees.add(doctorFee);
                    }
                }
            }

            // 使用stream流对集合中的对象进行复制和转换
            List<TblEapBenefitDoctor> tblDoctors = doctors.stream().map(doctorDto -> {
                TblEapBenefitDoctor tblDoctor = new TblEapBenefitDoctor();
                // 使用BeanUtils复制属性，这里假设EapBenefitDoctorDto和TblEapBenefitDoctor有相同的属性名
                //1-源，2-到哪
                BeanUtils.copyProperties(doctorDto, tblDoctor);
                return tblDoctor; // 返回复制后的对象
            }).collect(Collectors.toList()); // 将流中的对象收集成列表
            //批量保存医生
            eapBenefitDoctorService.insertAll(tblDoctors);
            //批量保存医生费用数据
            eapBenefitDoctorFeeService.insertAll(fees);
        } else {
            throw new ParamException("Parameter abnormality");
        }
    }
}
